package demo1.web.servlet.userMaiapi;

import demo1.dao.Mapper.userMaiMapper;
import demo1.dao.sql.userMaisql;
import demo1.service.userMaiService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Objects;

@WebServlet("/userMaiRegister")
public class userMaiRegister extends HttpServlet {//注册商户
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String userName = req.getParameter("username");
        String userPwd = req.getParameter("password");
        resp.setContentType("text/html;charset = utf-8");
        PrintWriter writer = resp.getWriter();

        ResultSet rs;
        try {
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String olduserName;
        if (!userPwd.trim().isEmpty()) {//密码不为空

            try {
                userMaiService check = new userMaiService();
                int num = check.checkName(userName);
                if(num == 0){
                    //如果有相同的用户名
                    writer.write("用户名重复了");
                }
                else {
                    userMaiService insert = new userMaiService();
                    insert.insertUserMai(userName,userPwd);//实现注册
                    resp.sendRedirect("registerSuccess.jsp");
                }

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        else {//密码为空
            writer.write("用户名密码不能为空");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
